<?php

/**
 * 自动生成 MerWxSettingController
 * 
 * Apps/Mp/Controller/MerWxSettingController.class.php
 * admin @ 2015-06-16
 */

namespace Mp\Controller;

use Common\Service\MerchantService ;
use Common\Service\MerWxSettingService;
use Common\Service\SysFuncService;

class MerWxSettingController extends MpController {

  public function index() {
    $action = I('get.action', 'read');

    $data = $this->_init_data();
    $data['module_name'] = '公众号管理';
    $data['module_url'] = U( $this->base_path . 'index');
    
    $MerWxSetting = MerWxSettingService::instance();
    $data['config'] = array(
      'action' => $action,
      'action_label' => $this->action_label[$action],
      'menu_id' => $this->menu_id ,
      'mer_id' => $this->mer_id ,
      'module_url' => U($this->base_path . 'index'),
      'read_url'   => U($this->base_path . 'read' ),
      'create_url' => U($this->base_path . 'create'),
      'update_url' => U($this->base_path . 'update'),
      'delete_url' => U($this->base_path . 'delete'),
      'page' => 1,
      'page_size' => C('PAGE_SIZE'),
      'row' => $MerWxSetting->get_default_row() 
    );

    $data['param'] = array(
      'type' => $MerWxSetting->get_status()
    );

    $this->display_with_layout($data, $this->controller .':index');
  }

  function read() {
    $post = I('post.');

    $config = array(
      'mer_id' => $this->mer_id ,
      'page' => 0 ,
      'sort' => empty($post['sort']) ? 'id' : $post['sort'],
      'order' => empty($post['order']) ? 'DESC' : $post['order'],
    );

    $MerWxSetting = MerWxSettingService::instance();
    $data['plugins_url'] = U('Mp/MerWxSetting/index_wx_func') .'/';
    $data['data'] = $MerWxSetting->get_by_cond($config);
    $data['type'] = $MerWxSetting->get_status();
    
    $this->assign($data);
    
    $data['html'] = $this->fetch('item');
    $this->ajaxReturn($data);
  }

  function create() {
    $post = I('post.');
    if (empty($post)) {
      $this->ajaxReturn(ajax_arr('数据错误,请稍后再试'));
    }
    
    $MerWxSetting = MerWxSettingService::instance();
    $ret = $MerWxSetting->create($post);
    $this->ajaxReturn($ret);
  }
  
  function update($id = 0) {
    $post = I('post.');
    if (empty($post)) {
      $this->ajaxReturn(ajax_arr('数据错误,请稍后再试'));
    }

    $MerWxSetting = MerWxSettingService::instance();
    $ret = $MerWxSetting->update($id, $post);
    $this->ajaxReturn($ret);
  }

  function delete() {
    $ids = I('post.ids');
    $MerWxSetting = MerWxSettingService::instance();
    $ret = $MerWxSetting->delete($ids); 
    $this->ajaxReturn($ret);
  }
  
  function index_wx_func( $wx_id ) {
    $mer_id = $this->mer_id ;
    
    $data = $this->_init_data();
    $data['module_name'] = '公众号管理';
    $data['module_url'] = U( $this->base_path . 'index');

    $data['config'] = array(
      'menu_id' => 'menu_Mp_MerWxSetting_index' ,
      'mer_id' => $this->mer_id ,
      'wx_id' => $wx_id ,
    );
    
    //根据角色 取微信功能表 
    $SysFunc = SysFuncService::instance();
    $data['wx_func'] = $SysFunc->get_wx_func_by_role($this->auth['role_id']);
    
    $Merchant = MerchantService::instance() ;
    $data['merchant_data'] = $Merchant->get_by_id( $mer_id );
    if ( empty( $data['merchant_data'] ) ) {
      gameover('商户不存在');
    }
    
    $MerWxSetting = MerWxSettingService::instance();
    $data['wx_data'] = $MerWxSetting->get_by_id( $wx_id );
    if ( $data['wx_data']['mer_id'] != $mer_id ) {
      gameover('未知的微信号');
    }

    $this->display_with_layout($data, $this->controller .':index_wx_func'); 
  }
}
